/*
Simple Forum Auto Poster.
Copyright (C) 2011 Gianni Van Hoecke.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package gui.forums;

import domain.DomainController;
import gui.forums.addedit.AddEditForumFrame;
import domain.enums.Action;
import domain.forumdata.Forum;
import domain.settings.Settings;
import java.util.logging.Level;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

/**
 *
 * @author Gianni Van Hoecke <gianni.vh@gmail.com>
 */
public class ForumListActionPanel extends javax.swing.JPanel {

    /** Creates new form ForumListActionPanel */
    public ForumListActionPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cmdAdd = new javax.swing.JButton();
        cmdEdit = new javax.swing.JButton();
        cmdRemove = new javax.swing.JButton();

        setLayout(new java.awt.GridLayout(1, 3));

        cmdAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/add_32.png"))); // NOI18N
        cmdAdd.setToolTipText("Add");
        cmdAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdAddActionPerformed(evt);
            }
        });
        add(cmdAdd);

        cmdEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/edit_32.png"))); // NOI18N
        cmdEdit.setToolTipText("Edit");
        cmdEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEditActionPerformed(evt);
            }
        });
        add(cmdEdit);

        cmdRemove.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/remove_32.png"))); // NOI18N
        cmdRemove.setToolTipText("Remove");
        cmdRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdRemoveActionPerformed(evt);
            }
        });
        add(cmdRemove);
    }// </editor-fold>//GEN-END:initComponents

    private void cmdAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAddActionPerformed

        //Only allow when there are categories...

        if(DomainController.getInstance().getCategories().getCategories().size() > 0)
            this.openAddEditFrame(Action.ADD);
        else
        {
            JOptionPane.showMessageDialog(this, "You need to add categories before you can add a forum."
                                                    + Settings.LINE_SEPARATOR
                                                    + "To do this, go to the settings tab.", "Information", JOptionPane.INFORMATION_MESSAGE);
        }

    }//GEN-LAST:event_cmdAddActionPerformed

    private void cmdEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEditActionPerformed

        if(DomainController.getInstance().getSelectedForums().length == 1)
            this.openAddEditFrame(Action.EDIT);
        else
        {
            DomainController.getInstance().getLogger().log(Level.INFO, "You can only edit with 1 selection.");
        }
        
    }//GEN-LAST:event_cmdEditActionPerformed

    private void cmdRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdRemoveActionPerformed

        if(JOptionPane.showConfirmDialog(null, "Do you want to remove the selected forums?", "Remove", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION)
        {
            String[] selectedForums = DomainController.getInstance().getSelectedForums();

            for(int i = selectedForums.length - 1; i >= 0; i--)
            {
                Forum forum = DomainController.getInstance().getForums().getForumByName(selectedForums[i]);
                DomainController.getInstance().getForums().removeForum(forum);
            }
        }

    }//GEN-LAST:event_cmdRemoveActionPerformed

    private void openAddEditFrame(final Action action)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                AddEditForumFrame aeff = new AddEditForumFrame(action);
                aeff.setLocationRelativeTo(null);
                aeff.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdAdd;
    private javax.swing.JButton cmdEdit;
    private javax.swing.JButton cmdRemove;
    // End of variables declaration//GEN-END:variables

    public void checkButtons()
    {
        this.cmdEdit.setEnabled(DomainController.getInstance().getForums().getForums().size() > 0);
        this.cmdRemove.setEnabled(DomainController.getInstance().getForums().getForums().size() > 0);
    }

    public void setButtonsEnabled(boolean enabled)
    {
        this.cmdAdd.setEnabled(enabled);
        this.cmdEdit.setEnabled(enabled);
        this.cmdRemove.setEnabled(enabled);
    }
}